package com.test.controller;

import com.test.bean.Result;
import com.test.bean.Student;
import com.test.bean.User;
import com.test.service.UserService;
import com.test.utils.ImportExcelUtil;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("student")

public class StudentController {

    @Autowired
    private UserService userService;
    private Student FileUtils;

    //    单条学生信息录入
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result AddStudent(Student student, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        try {
            userService.addStudent(student);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    excel表格学生信息录入
    @ResponseBody
    @RequestMapping(value = "adds",method = RequestMethod.POST)
    public class LeadingExcelController {

    /**
     * 描述：通过传统方式form表单提交方式导入excel文件
     *
     * @param request
     * @throws Exception
     */
        @RequestMapping("/form")
        public String form(HttpServletRequest request) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            InputStream in = null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");

            if (file.isEmpty()) {
                throw new Exception("文件不存在！");
            }
            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
            in.close();

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                Student student = new Student();
                student.setId(Integer.parseInt(String.valueOf(lo.get(0))));
                student.setSclass(Integer.parseInt(String.valueOf(lo.get(1))));
                student.setSname(String.valueOf(lo.get(2)));
                student.setGender(String.valueOf(lo.get(3)));
                student.setMajor(Integer.parseInt(String.valueOf(lo.get(4))));
                student.setInstructor(Integer.parseInt(String.valueOf(lo.get(5))));
                student.setDorm(Integer.parseInt(String.valueOf(lo.get(6))));
                student.setTelephone(Integer.parseInt(String.valueOf(lo.get(7))));
                student.setNation(String.valueOf(lo.get(8)));
                student.setPolitics_status(String.valueOf(lo.get(9)));
                student.setBirth_year(String.valueOf(lo.get(10)));
                student.setBirth_month(String.valueOf(lo.get(11)));
                student.setNative_place(String.valueOf(lo.get(12)));
                student.setLive_place(String.valueOf(lo.get(13)));
                student.setMailbox(String.valueOf(lo.get(14)));
                student.setEnrollment(String.valueOf(lo.get(15)));

            }

            return null;
        }

    /**
     * 描述：通过 jquery.form.js 插件提供的ajax方式上传文件
     * @param request
     * @param response
     * @throws Exception
     */
        @ResponseBody
        @RequestMapping(value="ajaxUpload.do",method={RequestMethod.GET,RequestMethod.POST})
        public  Result  ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            Result result = new Result();
//            System.out.println("通过 jquery.form.js 提供的ajax方式上传文件！");

            InputStream in =null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");
            if(file.isEmpty()){
                result.setCode(400);
                result.setMessage("excel解析录入失败");
            }

            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                Student student = new Student();
                student.setId(Integer.parseInt(String.valueOf(lo.get(0))));
                student.setSclass(Integer.parseInt(String.valueOf(lo.get(1))));
                student.setSname(String.valueOf(lo.get(2)));
                student.setGender(String.valueOf(lo.get(3)));
                student.setMajor(Integer.parseInt(String.valueOf(lo.get(4))));
                student.setInstructor(Integer.parseInt(String.valueOf(lo.get(5))));
                student.setDorm(Integer.parseInt(String.valueOf(lo.get(6))));
                student.setTelephone(Integer.parseInt(String.valueOf(lo.get(7))));
                student.setNation(String.valueOf(lo.get(8)));
                student.setPolitics_status(String.valueOf(lo.get(9)));
                student.setBirth_year(String.valueOf(lo.get(10)));
                student.setBirth_month(String.valueOf(lo.get(11)));
                student.setNative_place(String.valueOf(lo.get(12)));
                student.setLive_place(String.valueOf(lo.get(13)));
                student.setMailbox(String.valueOf(lo.get(14)));
                student.setEnrollment(String.valueOf(lo.get(15)));

//                System.out.println("打印信息-->机构:"+vo.getCode()+"  名称："+vo.getName()+"   时间："+vo.getDate()+"   资产："+vo.getMoney());
            }

            result.setCode(200);
            result.setMessage("excel解析录入成功");
            PrintWriter out = null;
            response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
            out = response.getWriter();
//            out.print("文件导入成功！");
            out.flush();
            out.close();
            return result;
        }

    }


//    学生信息删除  根据学号查找学生，找到删除，没找到报错
    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.DELETE)
    public Result DeleteStudent(Student student,int ids,HttpSession session,HttpServletResponse response){
        Result result = new Result();
        Student dstudent = userService.queryStudent(ids);
        if(dstudent != null){
            try {
                userService.deleteStudent(student);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    学生信息修改
    @ResponseBody
    @RequestMapping(value = "change",method = RequestMethod.PUT)
    public Result ChangeStudent(Student student,int id,HttpSession session,HttpServletResponse response){
        Result result = new Result();
        Student dstudent = userService.queryStudent(id);
        if(dstudent != null){
            try {
                userService.changeStudent(student);
                result.setMessage("修改成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();//在日志中输出错误
                result.setMessage("修改失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("修改失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    管理员查询学生信息 在对应的参数下输入查找的条件 返回所有符合条件的内容
    @ResponseBody
    @RequestMapping(value = "query",method = RequestMethod.GET)
    public List<queryStudent> QueryStudents(Student student) {
        Result result = new Result();
        List<queryStudent> list = new ArrayList<queryStudent>();
        try {
            list = userService.queryStudents(student);
            result.setMessage("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return list;
    }

//    学生查询自身信息
    @ResponseBody
    @RequestMapping(value = "student_query",method = RequestMethod.GET)
    public List<queryStudent> QueryStudent(int id) {
        Result result = new Result();
        List<queryStudent> list = new ArrayList<queryStudent>();
        try {
            list = userService.queryStudent1(id);
            result.setMessage("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return list;
    }
}
